package cn.iocoder.yudao.module.erp.controller.admin.pristock;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.pristock.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.pristock.PriStockDO;
import cn.iocoder.yudao.module.erp.service.pristock.PriStockService;

@Tag(name = "管理后台 - ERP 一级库库存")
@RestController
@RequestMapping("/erp/pri-stock")
@Validated
public class PriStockController {

    @Resource
    private PriStockService priStockService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 一级库库存")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:create')")
    public CommonResult<Long> createPriStock(@Valid @RequestBody PriStockSaveReqVO createReqVO) {
        return success(priStockService.createPriStock(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 一级库库存")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:update')")
    public CommonResult<Boolean> updatePriStock(@Valid @RequestBody PriStockSaveReqVO updateReqVO) {
        priStockService.updatePriStock(updateReqVO);
        return success(true);
    }


    @PostMapping("/inupdate")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:create')")
    public CommonResult<Boolean> inupdatePriStock(@RequestBody PriStockSaveReqVO updateReqVO) {
        priStockService.inupdatePriStock(updateReqVO);
        return success(true);
    }
    @PutMapping("/updates")
    @Operation(summary = "更新ERP 一级库库存")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:update')")
    public CommonResult<Boolean> updatePriStocks(@RequestBody PriStockSaveReqVO priStockSaveReqVO) {
        priStockService.updatePriStocks(priStockSaveReqVO);
        return success(true);
    }

    @PutMapping("/updatesdelect")
    @Operation(summary = "更新ERP 一级库库存")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:update')")
    public CommonResult<Boolean> updatePriStocksdelete(@RequestBody List<PriStockSaveReqVO> priStockSaveReqVO) {
        priStockService.updatePriStocksdelete(priStockSaveReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 一级库库存")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:delete')")
    public CommonResult<Boolean> deletePriStock(@RequestParam("id") Long id) {
        priStockService.deletePriStock(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 一级库库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<PriStockRespVO> getPriStock(@RequestParam("id") Long id) {
        PriStockDO priStock = priStockService.getPriStock(id);
        return success(BeanUtils.toBean(priStock, PriStockRespVO.class));
    }

    @GetMapping("/getProductIds")
    @Operation(summary = "获得ERP 一级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<List<PriStockRespVO>> getProductIds() {
        List<PriStockRespVO> productIds = priStockService.getProductIds();
        return success(BeanUtils.toBean(productIds, PriStockRespVO.class));
    }

    @GetMapping("/getIds")
    @Operation(summary = "获得ERP 一级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<List<Integer>> getIds() {
        List<Integer> productIds = priStockService.getIds();
        return success(productIds);
    }

    @GetMapping("/getProductBatchsByProductId")
    @Operation(summary = "获得ERP 一级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<List<String>> getProductBatchsByProductId(@RequestParam("productId") Long productId) {
        List<String> batchs = priStockService.getBatchsByProductId(productId);
        return success(batchs);
    }

//    @GetMapping("/getInboundBatchsByProductId")
//    @Operation(summary = "获得ERP 一级库库存批次列表")
//    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
//    public CommonResult<List<String>> getInboundBatchsByProductId(@RequestParam("productId") Long productId, @RequestParam("productBatch") String productBatch) {
//        List<String> batchs = priStockService.getInboundBatchsByProductId(productId,productBatch);
//        return success(batchs);
//    }

    @GetMapping("/getStandardsByProductName")
    @Operation(summary = "获得ERP er级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<List<String>> getStandardsByProductName(@RequestParam("productName") String productName) {
        List<String> standards = priStockService.getStandardsByProductName(productName);
        return success(standards);
    }

    @GetMapping("/getProductByBatch")
    @Operation(summary = "获得ERP 一级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<PriStockRespVO> getProductByBatch(@RequestParam("productId") Long productId, @RequestParam("productBatch") String productBatch) {
        PriStockDO priStock = priStockService.getProductByBatch(productId,productBatch);
        return success(BeanUtils.toBean(priStock, PriStockRespVO.class));
    }

    @GetMapping("/getList")
    @Operation(summary = "获得ERP 一级库库存列表")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<List<PriStockRespVO>> getPriStockList() {
        List<PriStockDO> priStockList = priStockService.getPriStockList();
        return success(BeanUtils.toBean(priStockList, PriStockRespVO.class));
    }

    @GetMapping("/getListByBatch")
    @Operation(summary = "获得ERP 一级库库存列表")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<List<PriStockRespVO>> getPriStockListByBatch() {
        List<PriStockDO> priStockList = priStockService.getPriStockListByBatch();
        return success(BeanUtils.toBean(priStockList, PriStockRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 一级库库存分页")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:query')")
    public CommonResult<PageResult<PriStockRespVO>> getPriStockPage(@Valid PriStockPageReqVO pageReqVO) {
        PageResult<PriStockDO> pageResult = priStockService.getPriStockPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PriStockRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 一级库库存 Excel")
    @PreAuthorize("@ss.hasPermission('erp:pri-stock:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPriStockExcel(@Valid PriStockPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PriStockDO> list = priStockService.getPriStockPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 一级库库存.xls", "数据", PriStockRespVO.class,
                BeanUtils.toBean(list, PriStockRespVO.class));
    }









}